package com.huzz.properties;

import com.huzz.util.RsaUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Map;

/**
 * RsaKey属性类
 *
 * @author Huzz
 * @create 2022-08-18 11:44
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "sigin-in", ignoreInvalidFields = true)
public class Properties {

    /**
     * 密钥配置信息
     */
    private Map<String, String> rsaKey;

    /**
     * 密钥
     */
    private PublicKey publicKey;
    private PrivateKey privateKey;

    /**
     * 是否开启Redis缓存(必须与认证服务器保存一致)
     */
    @Value("${sigin-in.token.onCacheToken}")
    private boolean onCacheToken;

    /**
     * Token续命(分钟)
     */
    @Value("${sigin-in.token.expireLifeExt}")
    private int expireLifeExt;

    /**
     * 该方法用于初始化公钥和私钥的内容
     */
    @PostConstruct
    public void loadRsaKey() throws Exception {
        if (rsaKey.containsKey("publicKeyPath") && rsaKey.get("publicKeyPath") != null) {
            publicKey = RsaUtils.getPublicKey(rsaKey.get("publicKeyPath"));
        }
        if (rsaKey.containsKey("privateKeyPath") && rsaKey.get("privateKeyPath") != null) {
            privateKey = RsaUtils.getPrivateKey(rsaKey.get("privateKeyPath"));
        }
    }

}
